<template>
	<view class="followLayout">
		<view class="left" @click="goUserInfo(item.id)">
			<view class="avatar">
				<image :src="item.avator" mode="aspectFill"></image>
			</view>
			<view class="info">
				<view class="nickname">{{item.nickname}}</view>
				<view class="inline_time">最近在线: {{item.last_time}}</view>
			</view>
		</view>
		<view class="right">
			<button size="mini" @click="toggleFollow(item)">{{isFollow ? "已关注" : "关注"}}</button>
		</view>
	</view>
</template>

<script setup>
	import {
		ref
	} from 'vue';
	import {
		apiToggleFollow
	} from '@/api/apis';

	defineProps(['item'])
	let isFollow = ref(true)
	const goUserInfo = (id) => {
		uni.navigateTo({
			url: "/pages/user/user?id=" + id
		})
	}
	const toggleFollow = (item) => {
		uni.showModal({
			title: "温馨提示",
			content: `确定要取消关注 [${item.nickname}] 吗`,
			success: async (res) => {
				if (res.confirm) {
					await apiToggleFollow({
						id: item.id
					}).then(res => {
						if (res.code == 200) {
							isFollow.value = !isFollow.value
							uni.showToast({
								title: '操作成功',
								icon: "none"
							})
						}
					})
				}
			}
		})
	}
</script>

<style lang="scss" scoped>
	.followLayout {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 10rpx;

		.left {
			display: flex;
			align-items: center;

			.avatar {
				width: 60rpx;
				height: 60rpx;
				overflow: hidden;
				border-radius: 50%;
				margin-right: 20rpx;

				image {
					width: 100%;
					height: 100%;
				}
			}

			.info {
				.inline_time {
					color: #6b7280;
					font-size: 20rpx;
				}
			}
		}
	}
</style>